package com.kevin.spring.aspectj;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;

/**
 * Created by loli on 2015/6/16.
 */
@Aspect
public class AspectJAdvice {
    @Pointcut("execution(* com.kevin.spring.aspectj.PersonService.*(..))")
    public void pointCut(){
    }

    @Before("pointCut()")
    public void beforeAdvice(JoinPoint joinPoint) {
        System.out.println("Before: " + joinPoint.getSignature().getName());
    }

    @After(value = "pointCut()")
    public void afterAdvice(JoinPoint joinPoint) {
        System.out.println("After: " + joinPoint.getSignature().getName());
    }

    @Around(value = "pointCut()")
    public Object aroundAdvice(ProceedingJoinPoint joinPoint) throws Throwable {
        System.out.println("Around: " + joinPoint.getSignature().getName());
        System.out.println("Before");
        Object obj = joinPoint.proceed();
        System.out.println("End");
        return obj;
    }

    @AfterReturning(value = "pointCut()", returning = "retVal")
    public void afterReturningAdvice(JoinPoint joinPoint, String retVal) {
        System.out.println("AfterReturning: "
                + joinPoint.getSignature().getName());
        System.out.println("Return Value: " + retVal);
    }

    @AfterThrowing(value = "pointCut()", throwing = "e")
    public void afterThrowingAdvice(JoinPoint joinPoint, Exception e) {
        System.out.println("AfterThrowing: "
                + joinPoint.getSignature().getName());
        System.out.println("Exception Message: " + e.getMessage());
    }
}
